﻿namespace Huayou.Utils
{
    using System;
    using System.Collections;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public class WebUtils
    {
        private Hashtable hs = null;

        public void add(string key, string keyValue)
        {
            if (this.hs == null)
            {
                this.hs = new Hashtable();
            }
            this.hs.Add(key, keyValue);
        }

        public static string GetUrlPath(string path)
        {
            return path.Substring(0, path.LastIndexOf("/"));
        }

        public string Pack()
        {
            return Pack(this.hs);
        }

        public static string Pack(Hashtable hs)
        {
            if ((hs == null) || (hs.Count == 0))
            {
                return null;
            }
            StringBuilder builder = null;
            IDictionaryEnumerator enumerator = hs.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (builder == null)
                {
                    builder = new StringBuilder();
                }
                else
                {
                    builder.Append("&");
                }
                builder.Append(enumerator.Key + "=" + HttpUtility.UrlEncode(enumerator.Value.ToString()));
            }
            return HttpUtility.UrlEncode(builder.ToString());
        }

        public static Hashtable UnPack(string strPack)
        {
            if (strPack == null)
            {
                return null;
            }
            string[] strArray = Regex.Split(HttpUtility.UrlDecode(strPack), "&");
            Hashtable hashtable = new Hashtable();
            for (int i = 0; i < strArray.Length; i++)
            {
                string[] strArray2 = Regex.Split(strArray[i], "=");
                if (strArray2.Length >= 2)
                {
                    hashtable.Add(strArray2[0], HttpUtility.UrlDecode(strArray2[1]));
                }
            }
            return hashtable;
        }
    }
}

